//
//  PersonalChangePasswordViewController.m
//  项目二
//
//  Created by rimi1 on 2016/11/7.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "PersonalChangePasswordViewController.h"
#import "ChangePasswordTableViewCell.h"
#import "ChangePasswordFootView.h"

#import "AVRequest+ChangeInfo.h"

static NSString *reusing_ = @"changePassViewCell";

@interface PersonalChangePasswordViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) ChangePasswordFootView *footView;

@end

@implementation PersonalChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.dataArr = @[@"旧密码", @"新密码", @"确认密码"];
}

- (void)initializeAppearance {
    self.navigationBar.title.text = @"修改密码";
    CGFloat width = 80;
    self.navigationBar.right.frame = CGRectMake(kWIDTH - width - 10, 20, width, 44);
    [self.navigationBar.right setTitle:@"显示密码" forState:UIControlStateNormal];
    self.tableView.tableFooterView = self.footView;
    [self initializeFootView];
    [self.view addSubview:self.tableView];
}

- (void)initializeFootView {
    CGRect rect = self.footView.frame;
    rect.size.height = kHEIGHT - CGRectGetMinY(self.footView.frame) - 64;
    self.footView.frame = rect;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - URLRequest

- (void)urlChangePasswordWithNewPassword:(NSString *)password {
    [AVRequest avPasswordWithPassword:password pSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"修改失败" message:nil];
            return;
        }
        
        if (succeed) {
            [Alert alertWithTitle:@"修改成功" message:nil];
            [MyUser sharedUser].password = password;
        }else {
            [Alert alertWithTitle:@"修改失败" message:nil];
        }
    }];
}

#pragma mark - Custom Method

- (void)sureChange {
    NSString *strOne = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).textField.text;
    NSString *strTwo = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]).textField.text;
    NSString *strThree = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]]).textField.text;
    
    if (strOne.length > 6 || strTwo.length > 6 || strThree.length > 6) {
        [Alert alertWithTitle:@"填写格式错误" message:nil];
    }else if (![strTwo isEqualToString:strThree]) {
        [Alert alertWithTitle:@"新密码不一致" message:nil];
    }else if (![[MyUser sharedUser].password isEqualToString:strOne]) {
        [Alert alertWithTitle:@"密码错误" message:nil];
    }else {
        [self urlChangePasswordWithNewPassword:strTwo];
    }
}

#pragma mark - respondsTo.

- (void)respondsToRight:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    UITextField *textOne = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).textField;
    UITextField *textTwo = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]).textField;
    UITextField *textThree = ((ChangePasswordTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]]).textField;
    
    BOOL is;
    if (sender.selected) {
        is = NO;
        [sender setTitle:@"隐藏密码" forState:UIControlStateNormal];
    }else {
        is = YES;
        [sender setTitle:@"显示密码" forState:UIControlStateNormal];
    }
    
    textOne.secureTextEntry = is;
    textTwo.secureTextEntry = is;
    textThree.secureTextEntry = is;
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChangePasswordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusing_ forIndexPath:indexPath];
    NSArray *arr = @[@"长度不能少于6位", @"长度不能少于6位", @"两次新密码必须一致"];
    cell.title.text = self.dataArr[indexPath.row];
    cell.textField.placeholder = arr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WXQAdaptionHeight(20 + 20 + 16);
}

#pragma mark - get

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.backgroundColor = kCOLOR_RGB(245, 245, 245, 1);
        [_tableView registerClass:[ChangePasswordTableViewCell class] forCellReuseIdentifier:reusing_];
        
    }
    return _tableView;
}

- (ChangePasswordFootView *)footView {
    
    if (!_footView) {
        
        _footView = [[ChangePasswordFootView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 0)];
        __weak PersonalChangePasswordViewController *change = self;
        _footView.changeBlock = ^() {
            [change sureChange];
        };
        
    }
    return _footView;
}

@end














